package lt.egidijus.aatest.utils;

import static org.junit.Assert.*;

import java.util.ArrayList;

import junit.framework.Assert;

import lt.egidijus.aatest.junit.JUnitFinder;

import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.TreeSelection;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class FileUtilsTest {

	ArrayList<ICompilationUnit> cList = null;
	ArrayList<ICompilationUnit> cList2 = null;
	

	@Before
	public void setUp() throws Exception {
		IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(Constants.projectName);
		JUnitFinder finder = new JUnitFinder();
		cList =  finder.findJUnitsForProject((JavaCore.create(project)));
		IProject project2 = ResourcesPlugin.getWorkspace().getRoot().getProject(Constants.testProjectName);
		cList2 =  finder.findJUnitsForProject((JavaCore.create(project2)));
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testGetClassName() {
		try {
	//		Assert.assertEquals(FileUtils.getClassName(cList.get(0)), "RangeBarChart");
		} catch (Exception e) {
			fail(e.getMessage());
		};
	}
	
	@Test
	public void testGetClassName2() {
		try {
	//		Assert.assertEquals(FileUtils.getClassName(cList2.get(0)), "ChartFactory");
		} catch (Exception e) {
			fail(e.getMessage());
		};
	}
	@Test
	public void testDeleteCoverageFolder() {
		try {
		FileUtils.deleteCoverageFolder();
		IProject iProject = ResourcesPlugin.getWorkspace().getRoot().getProject(Constants.testProjectName);
		IJavaProject project = JavaCore.create(iProject);	
		IFolder f = project.getProject().getFolder(Constants.coverageResourcesFolder);	
//		if (f.exists()) {
//			throw new Exception("Nepasalintas coverage katalogas");
//		}
		} catch (Exception e) {
			fail(e.getMessage());
		}
	}

	@Test
	public void testCreateCoverageFolder() {
		try {
			FileUtils.deleteCoverageFolder();
			FileUtils.createCoverageFolder();
			IProject iProject = ResourcesPlugin.getWorkspace().getRoot().getProject(Constants.testProjectName);
			IJavaProject project = JavaCore.create(iProject);	
//			IFolder f = project.getProject().getFolder(Constants.coverageResourcesFolder);	
//			if (!f.exists()) {
//				throw new Exception("Nesukurtas coverage katalogas");
//			}
			} catch (Exception e) {
				fail(e.getMessage());
			}
	}


	@Test
	public void testGetICompiliationUnit() {
		IProject iProject = ResourcesPlugin.getWorkspace().getRoot().getProject(Constants.testProjectName);
		IJavaProject project = JavaCore.create(iProject);
		ArrayList<ICompilationUnit> list = null;
		try {
			list = FileUtils.getICompiliationUnit(project.getAllPackageFragmentRoots()[0]);
	//		Assert.assertEquals(list.size(), 40);
		} catch (Exception e) {
			fail(e.getMessage());
		}
	}
	
	@Test
	public void testGetICompiliationUnit2() {
		IProject iProject = ResourcesPlugin.getWorkspace().getRoot().getProject(Constants.testProjectName);
		IJavaProject project = JavaCore.create(iProject);
		ArrayList<ICompilationUnit> list = null;
		try {
//			list = FileUtils.getICompiliationUnit(project.getAllPackageFragmentRoots()[0].getPackageFragment("org.achartengine"));
//			Assert.assertEquals(list.size(), 5);
		} catch (Exception e) {
			fail(e.getMessage());
		}
	}
	@Test
	public void testGetICompiliationUnit3() {
		ArrayList<ICompilationUnit> list = null;
		try {
//			list = FileUtils.getICompiliationUnit(cList.get(0));
//			Assert.assertEquals(list.size(), 1);
		} catch (Exception e) {
			fail(e.getMessage());
		}
	}
	@Test
	public void testGetICompiliationUnit4() {
		ArrayList<ICompilationUnit> list = null;
		try {
//			list = FileUtils.getICompiliationUnit(new Object());
//			Assert.assertEquals(list.size(), 0);
		} catch (Exception e) {
			fail(e.getMessage());
		}
	}

}
